<?php
/**
 * Model for News Component
 * @subpackage  news component
 * @license GNU/GPL
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');

/**
 * News Model
 *
 * @package    Joomla
 * @subpackage Components
 */
class JanewsModellayouttemplates extends JModel
{
	/**
	 * @return total templates
	 */
	function getTotal()
	{
		$db = JFactory::getDBO();
		$query = 'SELECT count(*) FROM #__janews_templates';
		$db->setQuery($query);
		$num = $db->loadResult();
		return $num;
	}
	
	/**
	 * 
	 */
	function createTemplate($templ, $type)
	{
	  $db = &JFactory::getDBO();
		$query = "SELECT count(*) FROM #__janews_templates WHERE template = '".$templ."' AND type = '".$type."'";
		$db->setQuery($query);
		if ($db->loadResult()== 0) {
		  $obj = new stdClass();
  		$obj->template = $templ;
  		$obj->title = $templ;
  		$obj->type = $type;
  		$db->insertObject('#__janews_templates', $obj);
		}
	}
	
	/**
	 * Update Templates
	 */
	
	/*function updateTemplates()
	{
	  $path = JPATH_COMPONENT_SITE.DS.'layouts';
	  $headlines = scandir($path.DS.'headline',1);
	  foreach ($headlines as $headline)
    {
      if (strpos($headline,'.')===false)
        $this->createTemplate($headline,'headline');
    }
    $blogs = scandir($path.DS.'blog',1);
	  foreach ($blogs as $blog)
    {
      if (strpos($blog,'.')===false)
        $this->createTemplate($blog,'blog');
    }
    
	}*/
	
	/**
	 * @return List layout templates order by type from database
	 */
	function getLists($group=null,$filter_order='', $filter_order_Dir='', $limit='', $limitstart='')
	{
	  $db = &JFactory::getDBO();
	  //update table template
	  //$this->updateTemplates();
		$query = "SELECT * FROM  #__janews_templates AS s ".
		($group ? "WHERE type ='$group'" : "").
		' ORDER BY '.$filter_order.' '. $filter_order_Dir .', s.type';
		if ($limit=='' && $limitstart==''){
			$db->setQuery( $query);
		}else{
			$db->setQuery( $query, $limitstart, $limit);
		}
		$list = $db->loadObjectList();
		return $list;
	}
	
	/**
	 * get Layouts available
	 */
	function getLayoutTemplates()
	{
	  $path = JPATH_COMPONENT_SITE.DS.'layouts';
	  $headlines = JFolder::folders($path.DS.'headline');
	  
	  //$headlines = scandir($path.DS.'headline',1);
	  $templates[0] = array( '0','-1','-Select Layout-' );
	  foreach ($headlines as $headline)
    {
      if (strpos($headline,'.')===false)
        $templates[] = array('headline', $headline, $headline);
    }
    
    $blogs = JFolder::folders($path.DS.'blog');
    //$blogs = scandir($path.DS.'blog',1);
	  foreach ($blogs as $blog)
    {
      if (strpos($blog,'.')===false)
        $templates[] = array('blog', $blog, $blog);
    }
   return $templates;
	}

	
	/**
	 * @return List of headlines or list of blogs
	 */
	function getListTemplates($type='headline')
	{
	  //$this->updateTemplates();
		$db = &JFactory::getDBO();
		$query = 'SELECT id AS value, title AS text FROM #__janews_templates WHERE type = "'.$type.'"';
		$db->setQuery($query);
		$list = $db->loadObjectList();
		asort($list);
		array_unshift($list, JHTML::_('select.option', '-1', '- '.JText::_('Select template to display').' -', 'value', 'text'));
		return $list;
	}
	
	
	/**
	 * @return list templates type for select 
	 */
	function getListType()
	{
		$arr = JArrayHelper::toObject( $a = array("value" => "0", "text" => "-Select template type-"));
		$headline = JArrayHelper::toObject( $a = array("value" => "headline", "text" => "headline"));
		$blog = JArrayHelper::toObject( $a = array("value" => "blog", "text" => "blog"));
		$rs = array("0" => $arr, "1" => $headline, "2" => $blog);
		return $rs;
	}
	
	/**
	 * @return Info a template
	 */
	function getInfoTempl($t_id)
	{
		if ($t_id) {
			$db = JFactory::getDBO();
			$query = 'SELECT * FROM #__janews_templates'
			.' WHERE id = '.$t_id;
			$db->setQuery($query);
			$rs = $db->loadObject();
		}
		return $rs;
	}
	
	/**
	 * @return title of the template
	 */
	function getTitleTempl($id)
	{
		$db = JFactory::getDBO();
		$query = 'SELECT title FROM #__janews_templates'
		.' WHERE id = '.$id;
		$db->setQuery($query);
		$rs = $db->loadResult();
		if (!$rs) $rs='';
		return $rs;
	}
	
}

?>